/*
 * Copyright (c) 2015 LingoChamp Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.liulishuo.filedownloader.database;

import com.liulishuo.filedownloader.model.ConnectionModel;
import com.liulishuo.filedownloader.model.FileDownloadModel;
import com.oszc.bbhmlibrary.database.OpenSqliteHelper;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.distributed.common.Value;
import ohos.data.rdb.*;


/**
 * The default opener of the filedownloader database helper.
 */
public class SqliteDatabaseOpenHelper extends OpenSqliteHelper {
    private static final String DATABASE_NAME = "filedownloader.db";
    private static final int DATABASE_VERSION = 1;

    public SqliteDatabaseOpenHelper(final Context context) {
        super(context, DATABASE_NAME,  DATABASE_VERSION);
    }
    @Override
    public void onCreate(RdbStore rdbStore) {
        rdbStore.executeSql("CREATE TABLE IF NOT EXISTS "
                + SqliteDatabaseImpl.TABLE_NAME + "( "
                + FileDownloadModel.ID + " INTEGER PRIMARY KEY, "  // id
                + FileDownloadModel.URL + " VARCHAR, "  // url
                + FileDownloadModel.PATH + " VARCHAR, "  // path
                + FileDownloadModel.STATUS + " TINYINT(7), "  // status
                + FileDownloadModel.SOFAR + " INTEGER, " // so far bytes
                + FileDownloadModel.TOTAL + " INTEGER, " // total bytes
                + FileDownloadModel.ERR_MSG + " VARCHAR, "  // error message
                + FileDownloadModel.ETAG + " VARCHAR, " // etag
                + FileDownloadModel.PATH_AS_DIRECTORY + " TINYINT(1) DEFAULT 0, "//path as directory
                + FileDownloadModel.FILENAME + " VARCHAR, " // path as directory
                + FileDownloadModel.CONNECTION_COUNT + " INTEGER DEFAULT 1" // connection count
                + ")");
        rdbStore.executeSql("CREATE TABLE IF NOT EXISTS "
                + SqliteDatabaseImpl.CONNECTION_TABLE_NAME + "( "
                + ConnectionModel.ID + " INTEGER, "
                + ConnectionModel.INDEX + " INTEGER, "
                + ConnectionModel.START_OFFSET + " INTEGER, "
                + ConnectionModel.CURRENT_OFFSET + " INTEGER, "
                + ConnectionModel.END_OFFSET + " INTEGER, "
                + "PRIMARY KEY ( " + ConnectionModel.ID + ", " + ConnectionModel.INDEX + " )"
                + ")");

        ValuesBucket values = new ValuesBucket();
        values.putInteger(ConnectionModel.END_OFFSET, -1);
        RdbPredicates rdbPredicates = new RdbPredicates(SqliteDatabaseImpl.CONNECTION_TABLE_NAME)
                .equalTo(ConnectionModel.END_OFFSET,0)
                .and()
                .greaterThan(ConnectionModel.START_OFFSET,0);
        rdbStore.update(values,rdbPredicates);

    }

    @Override
    public void onUpgrade(RdbStore rdbStore, int currentVersion, int targetVersion) {

    }

    @Override
    public void onDowngrade(RdbStore store, int currentVersion, int targetVersion) {
        super.onDowngrade(store, currentVersion, targetVersion);
        store.delete(new RdbPredicates(SqliteDatabaseImpl.TABLE_NAME));
        store.delete(new RdbPredicates(SqliteDatabaseImpl.CONNECTION_TABLE_NAME));
    }
}
